﻿using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using ECG.Common;
using ECG.DAL;
using ECG.WebUI.Models;

namespace ECG.WebUI.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/
        
        public ActionResult Index()
        {
            return View();
        }

        //top 20 tin moi nhat theo cate
        public ActionResult ListNewsCateHome(int id, int page = 1)
        {
            //lay chi tiet danh muc
            var model = NewsCategoryDb.GetAllNewsCategoryByType(id) ?? new NewsCategoryInfoDto();
            ViewBag.Page = "";
            if (page != 1)
                ViewBag.Page = "page " + page;
            return View(model);
        }

        [DonutOutputCache(Duration = 3600, VaryByParam = "page")]
        public PartialViewResult ListNewsHome(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["NewsListHomePageSize"]);
            var totalPage = 0;
            var model = NewsDb.GetAllNewsHomePaging(page, itemsPerPage, ref totalPage) ?? new List<NewsListHomeDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return PartialView(model);
        }

        

        //Theo danh muc tin
        [DonutOutputCache(Duration = 3600, VaryByParam = "id;page")]
        public PartialViewResult NewsListByCate(int id, int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["NewsListHomePageSize"]);
            var totalPage = 0;
            var model = NewsDb.GetAllNewsCatePaging(page, itemsPerPage, ref totalPage, id) ?? new List<NewsListHomeDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return PartialView(model);
        }

        //top 20 tin moi nhat theo cate        
        public ActionResult ListNewsTagHome(string id, int page = 1)
        {
            //lay chi tiet danh muc
            var model = TagDb.GetById(id) ?? new TagDto();
            ViewBag.Page = "";
            if (page != 1)
                ViewBag.Page = "page " + page;
            return View(model);
        }

        //theo Tag
        [DonutOutputCache(Duration = 3600, VaryByParam = "id;page")]
        public PartialViewResult NewsListByTag(string tagId, int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["NewsListHomePageSize"]);
            var totalPage = 0;
            var model = NewsDb.GetAllNewsTagPaging(page, itemsPerPage, ref totalPage, tagId) ?? new List<NewsListHomeDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return PartialView(model);
        }

        [DonutOutputCache(Duration = 3600, VaryByParam = "id")]
        public ActionResult NewsDetail(string id)
        {
            var model = NewsDb.GetById(id) ?? new NewsDto();
            MatchCollection mc;
            var r = new Regex(@"(?<=img[^>]*?src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22][^>]*?)");
            var hostForImage = ConfigurationManager.AppSettings["HostContainImages"];
            mc = r.Matches(model.Description);
            if (mc.Count > 0)
            {
                foreach (var item in mc)
                {
                    if (item == null || item.ToString().Contains("http")) continue;
                    if (!model.Description.Contains(hostForImage + item))
                    {
                        model.Description = model.Description.Replace(item.ToString(), hostForImage + item);
                    }
                }
            }
            return View(model);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public PartialViewResult BoxNewsCategory()
        {
            var model = NewsCategoryDb.GetAllNewsCategoryByType("news") ?? new List<NewsCategoryListMenuDto>();
            return PartialView(model);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public PartialViewResult BoxAboutCategory()
        {
            var model = NewsCategoryDb.GetAllNewsCategoryByType("about") ?? new List<NewsCategoryListMenuDto>();
            return PartialView(model);
        }

        //hien thi danh sach cac bai moi trong chi tiet tin
        public PartialViewResult NewsListOldTop(string Id)
        {
            var model = NewsDb.GetAllNewsNewTop(Id, "news") ?? new List<NewsListOldDto>();
            return PartialView(model);
        }

        //hien thi danh sach cac bai da dang trong chi tiet tin
        public PartialViewResult NewsListOldBottom(string Id)
        {
            var model = NewsDb.GetAllNewsOldTop(Id, "news") ?? new List<NewsListOldDto>();
            return PartialView(model);
        }

        //hien thi tag
        public PartialViewResult ShowTagByNewsId(string newsId)
        {
            var model = TagDb.GetTagNameByMapId(newsId) ?? new List<TagDto>();
            return PartialView(model);
        }
    }
}
